import json

# JSON文件的路径
file_path = './model.safetensors.index.json'

# 用于存储JSON数据的字典
data = {}

try:
    # 打开并读取JSON文件
    with open(file_path, 'r') as file:
        # 加载JSON数据
        data = json.load(file)

    # 打印读取到的数据
    print(json.dumps(data, indent=4))  # 使用indent参数美化输出格式

    # 访问特定的数据
    total_size = data['metadata']['total_size']
    print(f"Total Size: {total_size}")

    # 遍历weight_map字典
    for key, value in data['weight_map'].items():
        print(f"{key}: {value}")
        data['weight_map'][key]="01.safetensors"
        print(f"{key}: {value}")

    file_path = './my.json'

    try:
        # 将修改后的字典转换为JSON格式并保存到文件
        with open(file_path, 'w') as file:
            json.dump(data, file, indent=4)  # 使用indent参数美化输出格式

        print("JSON数据已更新并保存。")

    except Exception as e:
        print(f"保存JSON时发生错误: {e}")
except FileNotFoundError:
    print(f"The file {file_path} was not found.")
except json.JSONDecodeError:
    print("Error decoding JSON file.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")